package com.chujian.gateway.config;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.chujian.gateway.properties.DynamicRouteProperties;
import com.chujian.gateway.route.NacosRouteDefinitionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author ：chujian
 * @date ：2020-07-30
 * 动态路由配置，连接Nacos配置中心
 */
@Configuration
@EnableConfigurationProperties(DynamicRouteProperties.class)
public class DynamicRouteAutoConfiguration {

    @Autowired
    private ApplicationEventPublisher eventPublisher;

    @Autowired
    private DynamicRouteProperties dynamicRouteProperties;

    /**
     * Nacos 动态路由
     */
    @Configuration
    @ConditionalOnProperty(prefix = "chujian.gateway.dynamic-route", name = "sourceType", havingValue = "nacos", matchIfMissing = true)
    public class NacosDynRoute {
        @Autowired
        private NacosConfigManager nacosConfigManager;

        @Bean
        public NacosRouteDefinitionRepository nacosRouteDefinitionRepository() {
            return new NacosRouteDefinitionRepository(eventPublisher, nacosConfigManager, dynamicRouteProperties);
        }
    }

}
